#include "adminmainwindow.h"
#include "ui_adminmainwindow.h"
#include "ordermanage.h"
#include "coments_manage.h"
#include "product_manage.h"
#include "promotionmanagerwindow.h"
#include "salesstatisticswindow.h"
#include <QSqlError>
#include <QDebug>
#include <QMessageBox>

adminMainWindow::adminMainWindow(QSqlDatabase db, QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::adminMainWindow)
    , m_db(db)
    , m_promotionWindow(nullptr)
{
    ui->setupUi(this);
                if (!m_db.isOpen()) {
                qDebug() << "Database error:" << m_db.lastError();
}

                //        this->showNormal();  // 强制正常显示
                //        raise();  // 提到最前
                //        activateWindow();  // 激活窗口

                if (!m_db.isOpen()) {
                qDebug() << "数据库未打开:" << m_db.lastError().text();
                QMessageBox::critical(this, "错误", "数据库连接失败!");
} else {
                qDebug() << "数据库已连接";
                //        this->show();
                qDebug() << "退出adminMainWindow构造函数";
}


                    // 创建促销窗口实例（独立窗口，父窗口为 nullptr）
                    m_promotionWindow = new PromotionManagerWindow(m_db, nullptr);

                    // 监听促销窗口的返回信号
                    connect(m_promotionWindow, &PromotionManagerWindow::returnRequested, [this]() {
                        this->show();
                        m_promotionWindow->hide();
                    });
                            qDebug()<<"ni";
}

                adminMainWindow::~adminMainWindow()
    {
                delete ui;
                if (m_promotionWindow) {
                delete m_promotionWindow;
}

}

                void adminMainWindow::on_pushButtonOpenPromotion_clicked()
    {
                m_promotionWindow->show();  // 显示促销窗口
                this->hide();               // 隐藏主窗口
}


                void adminMainWindow::on_pushButtonOrder_manage_2_clicked()
    {
                OrderManage *m=new OrderManage(m_db,this);
                m->show();
                this->hide();

}

                void adminMainWindow::on_pushButtonUser_manage_2_clicked()
    {
                userManage *userWindow = new userManage(m_db, this);
                userWindow->show();
                this->hide();
}

                void adminMainWindow::on_pushButtonProducts_manage_2_clicked()
    {
                product_manage*m=new product_manage(m_db,this);
                m->show();
                this->hide();
}

                void adminMainWindow::on_pushButtonComments_clicked()
    {
                Coments_manage*m=new Coments_manage(m_db,this);
                m->show();
                this->hide();
}

                void adminMainWindow::on_salesButton_clicked()
    {
                SalesStatisticsWindow*m =new SalesStatisticsWindow(m_db,this);
                m->show();
                this->hide();
}
